package org.jeecg.modules.wlhy.hycfg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.modules.wlhy.hycfg.entity.HyCfgItem;
import org.jeecg.modules.wlhy.hycfg.mapper.HyCfgItemMapper;
import org.jeecg.modules.wlhy.hycfg.service.IHyCfgItemService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: hy_cfg_item
 * @Author: jeecg-boot
 * @Date:   2022-09-28
 * @Version: V1.0
 */
@Service
public class HyCfgItemServiceImpl extends ServiceImpl<HyCfgItemMapper, HyCfgItem> implements IHyCfgItemService {

    @Override
    public boolean checkTextExistByPid(String itemText, String pid) {
        return this.count(new QueryWrapper<HyCfgItem>().eq("pid",pid).eq("item_text",itemText)) > 0;
    }

    @Override
    public boolean checkTextExistByPidOutId(String itemText, String pid, String id) {
        return this.count(new QueryWrapper<HyCfgItem>()
                .eq("pid",pid)
                .eq("item_text",itemText)
                .ne("id", id)
        ) > 0;
    }

    @Override
    @Cacheable(unless="#result == null", cacheNames = CacheConstant.CACHE_HY_CFG_ITEM,key = "#cfgCode+':'+#itemText")
    public HyCfgItem getByCode(String cfgCode, String itemText) {
        return baseMapper.selectByCode(cfgCode,itemText);
    }

    @Override
    @CacheEvict(cacheNames= CacheConstant.CACHE_HY_CFG_ITEM, key="#cfgCode+ ':' + #hyCfgItem.itemText")
    public void edit(String cfgCode, HyCfgItem hyCfgItem) {
        baseMapper.updateById(hyCfgItem);
    }

    @Override
    @CacheEvict(cacheNames= CacheConstant.CACHE_HY_CFG_ITEM, key="#cfgCode+ ':' + #hyCfgItem.itemText")
    public void deleteByCode(String cfgCode, HyCfgItem hyCfgItem) {
        baseMapper.deleteById(hyCfgItem.getId());
    }
}
